package com.smugmugsoftware.airconcalc.advanced;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import android.content.Context;
import android.util.Log;

public class TableParser {

	private Context context;
	private Vector<String> table;

	public TableParser(Context context, int tableResource) {
		this.context = context;
		InputStream tableStream = context.getResources().openRawResource(tableResource);

		table = new Vector<String>();

		InputStreamReader reader = null;
		BufferedReader buffer = null;
		try {

			String line = null;
			reader = new InputStreamReader(tableStream);
			buffer = new BufferedReader(reader);

			do {
				line = buffer.readLine();
				if (line != null) {
					table.add(line);
				}
			} while (line != null);

		} catch (EOFException eof) {

		} catch (IOException ioe) {
			Log.e("Table Parser", "I/O Exception reading table file");
		} finally {
			try {
				buffer.close();
			} catch (Exception e) {}

			try {
				reader.close();
			} catch (Exception e) {}
		}
	}

	public String[] getIndex(String indexChar) {
		Iterator<String> iterator = table.iterator();
		String thisString = null;
		Vector<String> tempVector = new Vector<String>();
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();
			if (thisString.startsWith(indexChar)) {
				tempVector.add(thisString);
			}
		}

		return (String[]) tempVector.toArray(new String[tempVector.size()]);
	}

	public String[] getSubIndex(String[] subHeaders) {

		Iterator<String> iterator = table.iterator();
		String thisString = null;
		String indexString = null;
		String recordString = null;
		int currentIndex = 0;
		boolean record = false;
		boolean recording = false;
		Vector<String> tempVector = new Vector<String>();
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 3) {
				if (thisString != null && thisString.length() > 0 && indexString != null && thisString.startsWith(indexString)) {
					break;
				} else {
					continue;
				}
			}

			if (record && !recording) {
				recordString = thisString.substring(0, 2).trim();
				recording = true;
			}

			if (thisString.trim().length() > 2 && currentIndex < subHeaders.length && thisString.substring(1).trim().startsWith(subHeaders[currentIndex])) {
				if (currentIndex < subHeaders.length) {
					currentIndex++;
				}

				if (currentIndex == subHeaders.length) {
					indexString = thisString.substring(0, 2).trim();
					record = true;
				}
			}


			if (recording) {
				if (thisString.startsWith(recordString)) {
					if (thisString.indexOf(',') != -1) {
						tempVector.add(thisString.substring(1, thisString.indexOf(',')).trim());
					} else {
						tempVector.add(thisString.substring(1).trim());						
					}
				} else if (thisString.startsWith(indexString)) {
					break;
				}
			}
		}

		return (String[]) tempVector.toArray(new String[tempVector.size()]);
	}

	public String getItemNumber(String[] subHeaders) {

		Iterator<String> iterator = table.iterator();
		String thisString = null;
		String returnString = "";
		int currentIndex = 0;
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 1) {
				continue;
			}

			if (thisString.trim().length() > 2 && currentIndex < subHeaders.length && thisString.substring(1).trim().startsWith(subHeaders[currentIndex])) {

				currentIndex++;

				if (thisString.indexOf(",") != -1) {
					returnString = returnString + thisString.substring(thisString.indexOf(",") + 1).trim();
				}

				if (returnString.length() == 1 && returnString.indexOf("-") == -1 && (returnString.equals("O")
						|| returnString.equals("A") || returnString.equals("C"))) {
					returnString = returnString + "-";
				}

				if (returnString.length() == 2 && returnString.indexOf("-") == -1) {
					returnString = returnString + "-";
				}

				if (currentIndex == subHeaders.length) {
					return returnString;
				}
			}
		}
		return null;
	}

	public String getSingleItemNumber(String[] subHeaders) {

		Iterator<String> iterator = table.iterator();
		String thisString = null;
		String returnString = "";
		int currentIndex = 0;
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 1) {
				continue;
			}

			if (thisString.trim().length() > 2 && currentIndex < subHeaders.length && thisString.substring(1).trim().startsWith(subHeaders[currentIndex])) {

				currentIndex++;

				if (thisString.indexOf(",") != -1) {
					returnString = returnString + thisString.substring(thisString.indexOf(",") + 1).trim();
				}

				if (returnString.length() == 1 && returnString.indexOf("-") == -1) {
					returnString = returnString + "-";
				}

				if (returnString.length() == 2 && returnString.indexOf("-") == -1) {
					returnString = returnString + "-";
				}

				if (currentIndex == subHeaders.length) {
					return returnString;
				}
			}
		}
		return null;
	}

	public boolean checkItemExists(String itemNo, String type) {

		String firstHalf = null;
		String secondHalf = null;

		if (itemNo.startsWith("3")) {
			if (itemNo.indexOf("-") != -1) {
				firstHalf = itemNo.substring(0, itemNo.indexOf("-") - 1).trim();
				secondHalf = itemNo.substring(itemNo.indexOf("-") - 1, itemNo.indexOf("-")).trim().toUpperCase()
						+ itemNo.substring(itemNo.indexOf("-") + 1).trim().toUpperCase();
			} else if (itemNo.length() == 5) {
				firstHalf = itemNo.substring(0, 1).trim();
				secondHalf = itemNo.substring(1).trim().toUpperCase();
			} else {
				return false;
			}	

		} else {
			if (itemNo.indexOf("-") != -1) {
				firstHalf = itemNo.substring(0, itemNo.indexOf("-")).trim();
				secondHalf = itemNo.substring(itemNo.indexOf("-") + 1).trim().toUpperCase();
			} else if (itemNo.length() == 2) {
				firstHalf = itemNo.substring(0, 1).trim();
				secondHalf = itemNo.substring(1).trim().toUpperCase();
			} else if (itemNo.length() == 3) {
				firstHalf = itemNo.substring(0, 2).trim();
				secondHalf = itemNo.substring(2).trim().toUpperCase();
			} else {
				return false;
			}
		}

		String[] searchArray = { type, firstHalf, "Data", secondHalf };

		Iterator<String> iterator = table.iterator();
		String thisString = null;
		int currentIndex = 0;
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 3) {
				continue;
			}

			if (thisString.trim().length() > 2 && currentIndex < searchArray.length) {

				if ((currentIndex == 0 || currentIndex == 2) && thisString.substring(1).trim().equals(searchArray[currentIndex])) {
					currentIndex++;
				}

				if (currentIndex == 1 && thisString.indexOf(",") != -1 && thisString.substring(thisString.indexOf(",") + 1).trim().equals(searchArray[currentIndex])) {
					currentIndex++;
				}

				if (currentIndex == 3 && thisString.substring(1).trim().toUpperCase().equals(searchArray[currentIndex])) {
					currentIndex++;
				} else if (currentIndex == 3 && thisString.indexOf(",") != -1 && thisString.substring(1, thisString.indexOf(",")).trim().toUpperCase().equals(searchArray[currentIndex])) {
					currentIndex++;
				}

				if (currentIndex == searchArray.length) {
					return true;
				}
			}
		}
		return false;
	}

	public String getDataPoint(String itemNo, String type, int column) {
		String firstHalf = null;
		String secondHalf = null;
		String facingString = null;
		String[] searchArray = null;
		
		if (itemNo.startsWith("3")) {
			if (itemNo.indexOf("-") != -1) {
				firstHalf = itemNo.substring(0, itemNo.indexOf("-") - 1).trim();
				secondHalf = itemNo.substring(itemNo.indexOf("-") - 1, itemNo.indexOf("-")).trim().toUpperCase()
						+ itemNo.substring(itemNo.indexOf("-") + 1, itemNo.lastIndexOf("-")).trim().toUpperCase();
				facingString = itemNo.substring(itemNo.lastIndexOf("-") + 1);

				if (facingString.equals("NE") || facingString.equals("NW")) {
					facingString = "NN";
				}

				if (facingString.equals("E") || facingString.equals("W")) {
					facingString = "EW";
				}

				if (facingString.equals("SE") || facingString.equals("SW")) {
					facingString = "SS";
				}

			} else if (itemNo.length() == 5) {
				firstHalf = itemNo.substring(0, 1).trim();
				secondHalf = itemNo.substring(1, 4).trim().toUpperCase();
			} else {
				searchArray = new String[] { itemNo };
			}	

		} else {
			if (itemNo.indexOf("-") != -1) {
				firstHalf = itemNo.substring(0, itemNo.indexOf("-")).trim();
				secondHalf = itemNo.substring(itemNo.indexOf("-") + 1).trim().toUpperCase();
			} else if (itemNo.length() == 2) {
				firstHalf = itemNo.substring(0, 1).trim();
				secondHalf = itemNo.substring(1).trim().toUpperCase();
			} else if (itemNo.length() == 3) {
				firstHalf = itemNo.substring(0, 2).trim();
				secondHalf = itemNo.substring(2).trim().toUpperCase();
			} else {
				searchArray = new String[] { itemNo };
			}
		}

		if (type.equals(String.valueOf(NewRoomFragmentInterface.WINDOW_SHADE))) {
			searchArray = new String[] { itemNo };
		} else if (type != null && facingString != null) {
			searchArray = new String[] { type, firstHalf, "Data", secondHalf, facingString };
		} else if (type != null) {
			searchArray = new String[] { type, firstHalf, "Data", secondHalf };
		}

		Iterator<String> iterator = table.iterator();
		String thisString = null;
		int currentIndex = 0;
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 3) {
				continue;
			}

			if (thisString.trim().length() > 2 && currentIndex < searchArray.length) {

				if (type.equals(String.valueOf(NewRoomFragmentInterface.WINDOW_SHADE))) {
					if (thisString.indexOf(",") != -1 && thisString.substring(0, thisString.indexOf(",")).trim().equals(searchArray[currentIndex])) {
						currentIndex++;
					}					
				} else if (type != null) {
					if ((currentIndex == 0 || currentIndex == 2) && thisString.substring(1).trim().equals(searchArray[currentIndex])) {
						currentIndex++;
					}

					if (currentIndex == 1 && thisString.indexOf(",") != -1 && thisString.substring(thisString.indexOf(",") + 1).trim().equals(searchArray[currentIndex])) {
						currentIndex++;
					}

					if ((currentIndex == 3 || currentIndex == 4) && thisString.substring(1).trim().toUpperCase().equals(searchArray[currentIndex])) {
						currentIndex++;
					} else if ((currentIndex == 3 || currentIndex == 4) && thisString.indexOf(",") != -1 && thisString.substring(1, thisString.indexOf(",")).trim().toUpperCase().equals(searchArray[currentIndex])) {
						currentIndex++;
					}
				} else {
					if (thisString.indexOf(",") != -1 && thisString.substring(0, thisString.indexOf(",")).trim().equals(searchArray[currentIndex])) {
						currentIndex++;
					}
				}

				if (currentIndex == searchArray.length) {
					StringTokenizer tokenizer = new StringTokenizer(thisString, ",");
					for (int i = 0; i < column; i++) {
						tokenizer.nextToken();
					}
					return tokenizer.nextToken().trim();
				}
			}
		}
		return null;
	}

	public String[] getLocaleData(String[] locale) {

		Iterator<String> iterator = table.iterator();
		Vector<String> returnString = new Vector<String>();
		String thisString = null;
		int currentIndex = 1;
		while (iterator.hasNext()) {
			thisString = iterator.next().trim();

			if (thisString.length() < 3) {
				continue;
			}

			if (thisString.substring(2).trim().equals(locale[currentIndex])) {
				currentIndex++;
			} else if (thisString.indexOf(",") != -1 && thisString.substring(2, thisString.indexOf(",")).trim().equals(locale[currentIndex])) {
				currentIndex++;
			}

			if (currentIndex == locale.length) {
				StringTokenizer tokenizer = new StringTokenizer(thisString.substring(2), ",");
				while (tokenizer.hasMoreTokens()) {
					returnString.add(tokenizer.nextToken().trim());
				}

				return returnString.toArray(new String[returnString.size()]);
			}
		}
		return null;
	}
}
